/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package BoardGameToTreeIntegration.Rules.impl;

import BoardGame.Board;
import BoardGame.BoardGameFactory;
import BoardGame.Field;

import BoardGameToTreeIntegration.BoardGameToTreeIntegrationFactory;
import BoardGameToTreeIntegration.BoardToFile;
import BoardGameToTreeIntegration.FieldToNode;

import BoardGameToTreeIntegration.Rules.RowFieldRule;
import BoardGameToTreeIntegration.Rules.RulesPackage;

import MocaTree.File;
import MocaTree.MocaTreeFactory;
import MocaTree.Node;

import TGGLanguage.csp.*;

import TGGRuntime.Edge;
import TGGRuntime.IsAppropriateRuleResult;
import TGGRuntime.Match;
import TGGRuntime.PerformRuleResult;
import TGGRuntime.TGGRuntimeFactory;

import TGGRuntime.impl.AbstractRuleImpl;

import csp.constraints.*;

import de.upb.tools.sdm.*;

import java.lang.reflect.InvocationTargetException;

import java.util.*;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Row Field Rule</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class RowFieldRuleImpl extends AbstractRuleImpl implements RowFieldRule {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected RowFieldRuleImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return RulesPackage.Literals.ROW_FIELD_RULE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PerformRuleResult perform_FWD_Field_1(Field col) {
		boolean fujaba__Success = false;
		PerformRuleResult ruleResult = null;
		Node colNode = null;
		FieldToNode c2n = null;
		Edge c2n__target__colNode = null;
		Edge c2n__source__col = null;
		Edge col__board__board = null;
		Edge rowNode__children__colNode = null;
		File file = null;
		Node rowNode = null;
		Iterator fujaba__IterRowToR2n = null;
		FieldToNode r2n = null;
		Iterator fujaba__IterBoardToB2f = null;
		BoardToFile b2f = null;
		Iterator fujaba__IterBoardToRow = null;
		Field row = null;
		Board board = null;

		// story pattern prepare return value
		try {
			fujaba__Success = false;

			// create object ruleResult
			ruleResult = TGGRuntimeFactory.eINSTANCE.createPerformRuleResult();
			// assign attribute ruleResult
			ruleResult.setSuccess(false);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern find context
		try {
			fujaba__Success = false;

			// check object col is really bound
			JavaSDM.ensure(col != null);
			// bind object
			board = col.getBoard();

			// check object board is really bound
			JavaSDM.ensure(board != null);

			// iterate to-many link fields from board to row
			fujaba__Success = false;

			fujaba__IterBoardToRow = new ArrayList(board.getFields())
					.iterator();

			while (fujaba__IterBoardToRow.hasNext()) {
				try {
					row = (Field) fujaba__IterBoardToRow.next();

					// check object row is really bound
					JavaSDM.ensure(row != null);
					// check isomorphic binding between objects row and col
					JavaSDM.ensure(!row.equals(col));

					// iterate to-many link source from board to b2f
					fujaba__Success = false;

					fujaba__IterBoardToB2f = new ArrayList(
							org.moflon.util.eMoflonEMFUtil
									.getOppositeReference(board,
											BoardToFile.class, "source"))
							.iterator();

					while (fujaba__IterBoardToB2f.hasNext()) {
						try {
							b2f = (BoardToFile) fujaba__IterBoardToB2f.next();

							// check object b2f is really bound
							JavaSDM.ensure(b2f != null);
							// iterate to-many link source from row to r2n
							fujaba__Success = false;

							fujaba__IterRowToR2n = new ArrayList(
									org.moflon.util.eMoflonEMFUtil
											.getOppositeReference(row,
													FieldToNode.class, "source"))
									.iterator();

							while (fujaba__IterRowToR2n.hasNext()) {
								try {
									r2n = (FieldToNode) fujaba__IterRowToR2n
											.next();

									// check object r2n is really bound
									JavaSDM.ensure(r2n != null);
									// bind object
									rowNode = r2n.getTarget();

									// check object rowNode is really bound
									JavaSDM.ensure(rowNode != null);

									// bind object
									file = rowNode.getFile();

									// check object file is really bound
									JavaSDM.ensure(file != null);

									// check link target from b2f to file
									JavaSDM.ensure(file.equals(b2f.getTarget()));

									// Create CSP
									CSP csp = CspFactory.eINSTANCE.createCSP();

									// Create literals

									// Create implicit parameters
									Variable<Integer> var_row_y = CspFactory.eINSTANCE
											.createVariable("row.y", true, csp);
									var_row_y.setValue(row.getY());
									Variable<Integer> var_col_y = CspFactory.eINSTANCE
											.createVariable("col.y", true, csp);
									var_col_y.setValue(col.getY());

									// Create explicit parameters

									// Create unbound variables
									Variable<Integer> var_colNode_index = CspFactory.eINSTANCE
											.createVariable("colNode.index",
													csp);

									// Create constraints
									Eq<Integer> eq = new Eq<Integer>();
									Eq<Integer> eq_0 = new Eq<Integer>();

									csp.getConstraints().add(eq);
									csp.getConstraints().add(eq_0);

									// Solve CSP
									eq.solve(var_row_y, var_col_y);
									eq_0.solve(var_col_y, var_colNode_index);

									fujaba__Success = csp.check();
									if (fujaba__Success) {
										// story pattern perform transformation
										try {
											fujaba__Success = false;

											// check object b2f is really bound
											JavaSDM.ensure(b2f != null);
											// check object board is really bound
											JavaSDM.ensure(board != null);
											// check object col is really bound
											JavaSDM.ensure(col != null);
											// check object file is really bound
											JavaSDM.ensure(file != null);
											// check object r2n is really bound
											JavaSDM.ensure(r2n != null);
											// check object row is really bound
											JavaSDM.ensure(row != null);
											// check object rowNode is really bound
											JavaSDM.ensure(rowNode != null);
											// check isomorphic binding between objects row and col
											JavaSDM.ensure(!row.equals(col));

											// check link fields from col to board
											JavaSDM.ensure(board.equals(col
													.getBoard()));

											// check link fields from row to board
											JavaSDM.ensure(board.equals(row
													.getBoard()));

											// check link rootNode from file to rowNode
											JavaSDM.ensure(rowNode.equals(file
													.getRootNode()));

											// check link source from b2f to board
											JavaSDM.ensure(board.equals(b2f
													.getSource()));

											// check link source from r2n to row
											JavaSDM.ensure(row.equals(r2n
													.getSource()));

											// check link target from b2f to file
											JavaSDM.ensure(file.equals(b2f
													.getTarget()));

											// check link target from r2n to rowNode
											JavaSDM.ensure(rowNode.equals(r2n
													.getTarget()));

											// create object colNode
											colNode = MocaTreeFactory.eINSTANCE
													.createNode();
											// create object c2n
											c2n = BoardGameToTreeIntegrationFactory.eINSTANCE
													.createFieldToNode();
											// assign attribute colNode
											colNode.setName("FIELD");
											// assign attribute colNode
											colNode.setIndex(var_colNode_index
													.getValue());
											// create link
											colNode.setParentNode(rowNode);

											// create link
											c2n.setSource(col);

											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(c2n,
															colNode, "target");
											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										// story pattern collect translated elements
										try {
											fujaba__Success = false;

											// check object c2n is really bound
											JavaSDM.ensure(c2n != null);
											// check object col is really bound
											JavaSDM.ensure(col != null);
											// check object colNode is really bound
											JavaSDM.ensure(colNode != null);
											// check object ruleResult is really bound
											JavaSDM.ensure(ruleResult != null);
											// assign attribute ruleResult
											ruleResult.setSuccess(true);
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult, col,
															"translatedElements");
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult,
															colNode,
															"createdElements");
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult, c2n,
															"createdLinkElements");
											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										// story pattern bookkeeping for edges
										try {
											fujaba__Success = false;

											// check object b2f is really bound
											JavaSDM.ensure(b2f != null);
											// check object board is really bound
											JavaSDM.ensure(board != null);
											// check object c2n is really bound
											JavaSDM.ensure(c2n != null);
											// check object col is really bound
											JavaSDM.ensure(col != null);
											// check object colNode is really bound
											JavaSDM.ensure(colNode != null);
											// check object file is really bound
											JavaSDM.ensure(file != null);
											// check object r2n is really bound
											JavaSDM.ensure(r2n != null);
											// check object row is really bound
											JavaSDM.ensure(row != null);
											// check object rowNode is really bound
											JavaSDM.ensure(rowNode != null);
											// check object ruleResult is really bound
											JavaSDM.ensure(ruleResult != null);
											// check isomorphic binding between objects board and b2f
											JavaSDM.ensure(!board.equals(b2f));

											// check isomorphic binding between objects c2n and b2f
											JavaSDM.ensure(!c2n.equals(b2f));

											// check isomorphic binding between objects col and b2f
											JavaSDM.ensure(!col.equals(b2f));

											// check isomorphic binding between objects colNode and b2f
											JavaSDM.ensure(!colNode.equals(b2f));

											// check isomorphic binding between objects file and b2f
											JavaSDM.ensure(!file.equals(b2f));

											// check isomorphic binding between objects r2n and b2f
											JavaSDM.ensure(!r2n.equals(b2f));

											// check isomorphic binding between objects row and b2f
											JavaSDM.ensure(!row.equals(b2f));

											// check isomorphic binding between objects rowNode and b2f
											JavaSDM.ensure(!rowNode.equals(b2f));

											// check isomorphic binding between objects c2n and board
											JavaSDM.ensure(!c2n.equals(board));

											// check isomorphic binding between objects col and board
											JavaSDM.ensure(!col.equals(board));

											// check isomorphic binding between objects colNode and board
											JavaSDM.ensure(!colNode
													.equals(board));

											// check isomorphic binding between objects file and board
											JavaSDM.ensure(!file.equals(board));

											// check isomorphic binding between objects r2n and board
											JavaSDM.ensure(!r2n.equals(board));

											// check isomorphic binding between objects row and board
											JavaSDM.ensure(!row.equals(board));

											// check isomorphic binding between objects rowNode and board
											JavaSDM.ensure(!rowNode
													.equals(board));

											// check isomorphic binding between objects col and c2n
											JavaSDM.ensure(!col.equals(c2n));

											// check isomorphic binding between objects colNode and c2n
											JavaSDM.ensure(!colNode.equals(c2n));

											// check isomorphic binding between objects file and c2n
											JavaSDM.ensure(!file.equals(c2n));

											// check isomorphic binding between objects r2n and c2n
											JavaSDM.ensure(!r2n.equals(c2n));

											// check isomorphic binding between objects row and c2n
											JavaSDM.ensure(!row.equals(c2n));

											// check isomorphic binding between objects rowNode and c2n
											JavaSDM.ensure(!rowNode.equals(c2n));

											// check isomorphic binding between objects colNode and col
											JavaSDM.ensure(!colNode.equals(col));

											// check isomorphic binding between objects file and col
											JavaSDM.ensure(!file.equals(col));

											// check isomorphic binding between objects r2n and col
											JavaSDM.ensure(!r2n.equals(col));

											// check isomorphic binding between objects row and col
											JavaSDM.ensure(!row.equals(col));

											// check isomorphic binding between objects rowNode and col
											JavaSDM.ensure(!rowNode.equals(col));

											// check isomorphic binding between objects file and colNode
											JavaSDM.ensure(!file
													.equals(colNode));

											// check isomorphic binding between objects r2n and colNode
											JavaSDM.ensure(!r2n.equals(colNode));

											// check isomorphic binding between objects row and colNode
											JavaSDM.ensure(!row.equals(colNode));

											// check isomorphic binding between objects rowNode and colNode
											JavaSDM.ensure(!rowNode
													.equals(colNode));

											// check isomorphic binding between objects r2n and file
											JavaSDM.ensure(!r2n.equals(file));

											// check isomorphic binding between objects row and file
											JavaSDM.ensure(!row.equals(file));

											// check isomorphic binding between objects rowNode and file
											JavaSDM.ensure(!rowNode
													.equals(file));

											// check isomorphic binding between objects row and r2n
											JavaSDM.ensure(!row.equals(r2n));

											// check isomorphic binding between objects rowNode and r2n
											JavaSDM.ensure(!rowNode.equals(r2n));

											// check isomorphic binding between objects rowNode and row
											JavaSDM.ensure(!rowNode.equals(row));

											// create object c2n__target__colNode
											c2n__target__colNode = TGGRuntimeFactory.eINSTANCE
													.createEdge();
											// create object c2n__source__col
											c2n__source__col = TGGRuntimeFactory.eINSTANCE
													.createEdge();
											// create object col__board__board
											col__board__board = TGGRuntimeFactory.eINSTANCE
													.createEdge();
											// create object rowNode__children__colNode
											rowNode__children__colNode = TGGRuntimeFactory.eINSTANCE
													.createEdge();
											// assign attribute rowNode__children__colNode
											rowNode__children__colNode
													.setName("rowNode__children__colNode");
											// assign attribute col__board__board
											col__board__board.setName("board");
											// assign attribute c2n__source__col
											c2n__source__col
													.setName("c2n__source__col");
											// assign attribute c2n__target__colNode
											c2n__target__colNode
													.setName("c2n__target__colNode");
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult,
															c2n__target__colNode,
															"createdEdges");
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult,
															c2n__source__col,
															"createdEdges");
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult,
															col__board__board,
															"translatedEdges");
											// create link
											org.moflon.util.eMoflonEMFUtil
													.addOppositeReference(
															ruleResult,
															rowNode__children__colNode,
															"createdEdges");
											// create link
											rowNode__children__colNode
													.setSrc(rowNode);

											// create link
											col__board__board.setTrg(board);

											// create link
											c2n__source__col.setTrg(col);

											// create link
											col__board__board.setSrc(col);

											// create link
											c2n__target__colNode.setSrc(c2n);

											// create link
											c2n__source__col.setSrc(c2n);

											// create link
											rowNode__children__colNode
													.setTrg(colNode);

											// create link
											c2n__target__colNode
													.setTrg(colNode);

											fujaba__Success = true;
										} catch (JavaSDMException fujaba__InternalException) {
											fujaba__Success = false;
										}

										return ruleResult;

									}

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}
							}
							JavaSDM.ensure(fujaba__Success);

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}
					}
					JavaSDM.ensure(fujaba__Success);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return ruleResult;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public IsAppropriateRuleResult isAppropriate_FWD_Field_1(Field col) {
		boolean fujaba__Success = false;
		Object _TmpObject = null;
		EClass eClass = null;
		Iterator fujaba__IterEClassToPerformOperation = null;
		EOperation performOperation = null;
		IsAppropriateRuleResult ruleResult = null;
		Match match = null;
		Edge col__board__null = null;
		Iterator fujaba__IterBoardToRow = null;
		Field row = null;
		Board board = null;
		Iterator fujaba__IterRuleResultToMatch = null;

		// story pattern prepare return value
		try {
			fujaba__Success = false;

			_TmpObject = (this.eClass());

			// ensure correct type and really bound of object eClass
			JavaSDM.ensure(_TmpObject instanceof EClass);
			eClass = (EClass) _TmpObject;

			// iterate to-many link eOperations from eClass to performOperation
			fujaba__Success = false;

			fujaba__IterEClassToPerformOperation = eClass.getEOperations()
					.iterator();

			while (!(fujaba__Success)
					&& fujaba__IterEClassToPerformOperation.hasNext()) {
				try {
					performOperation = (EOperation) fujaba__IterEClassToPerformOperation
							.next();

					// check object performOperation is really bound
					JavaSDM.ensure(performOperation != null);
					// attribute condition
					JavaSDM.ensure(JavaSDM.stringCompare(
							performOperation.getName(), "perform_FWD_Field_1") == 0);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// create object ruleResult
			ruleResult = TGGRuntimeFactory.eINSTANCE
					.createIsAppropriateRuleResult();
			// assign attribute ruleResult
			ruleResult.setSuccess(false);
			// create link
			ruleResult.setPerformOperation(performOperation);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern test core match
		try {
			fujaba__Success = false;

			// check object col is really bound
			JavaSDM.ensure(col != null);
			// check object ruleResult is really bound
			JavaSDM.ensure(ruleResult != null);
			// bind object
			board = col.getBoard();

			// check object board is really bound
			JavaSDM.ensure(board != null);

			// iterate to-many link fields from board to row
			fujaba__Success = false;

			fujaba__IterBoardToRow = new ArrayList(board.getFields())
					.iterator();

			while (fujaba__IterBoardToRow.hasNext()) {
				try {
					row = (Field) fujaba__IterBoardToRow.next();

					// check object row is really bound
					JavaSDM.ensure(row != null);
					// check isomorphic binding between objects row and col
					JavaSDM.ensure(!row.equals(col));

					// create object match
					match = TGGRuntimeFactory.eINSTANCE.createMatch();
					// create link
					ruleResult.getMatches().add(match);

					// story pattern collect elements to be translated
					try {
						fujaba__Success = false;

						// check object board is really bound
						JavaSDM.ensure(board != null);
						// check object col is really bound
						JavaSDM.ensure(col != null);
						// check object match is really bound
						JavaSDM.ensure(match != null);
						// create object col__board__null
						col__board__null = TGGRuntimeFactory.eINSTANCE
								.createEdge();
						// assign attribute col__board__null
						col__board__null.setName("board");
						// create link
						org.moflon.util.eMoflonEMFUtil.addOppositeReference(
								match, col__board__null, "toBeTranslatedEdges");
						// create link
						org.moflon.util.eMoflonEMFUtil.addOppositeReference(
								match, col, "toBeTranslatedElements");
						// create link
						col__board__null.setSrc(col);

						// create link
						org.moflon.util.eMoflonEMFUtil.addOppositeReference(
								col__board__null, board, "trg");
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story pattern collect context nodes
					try {
						fujaba__Success = false;

						// check object board is really bound
						JavaSDM.ensure(board != null);
						// check object match is really bound
						JavaSDM.ensure(match != null);
						// check object row is really bound
						JavaSDM.ensure(row != null);
						// create link
						org.moflon.util.eMoflonEMFUtil.addOppositeReference(
								match, board, "contextElements");
						// create link
						org.moflon.util.eMoflonEMFUtil.addOppositeReference(
								match, row, "contextElements");
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern set success
		try {
			fujaba__Success = false;

			// check object ruleResult is really bound
			JavaSDM.ensure(ruleResult != null);
			// iterate to-many link matches from ruleResult to match
			fujaba__Success = false;

			fujaba__IterRuleResultToMatch = ruleResult.getMatches().iterator();

			while (!(fujaba__Success)
					&& fujaba__IterRuleResultToMatch.hasNext()) {
				try {
					match = (Match) fujaba__IterRuleResultToMatch.next();

					// check object match is really bound
					JavaSDM.ensure(match != null);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// assign attribute ruleResult
			ruleResult.setSuccess(true);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return ruleResult;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PerformRuleResult perform_BWD_Node_1(Node colNode) {
		boolean fujaba__Success = false;
		PerformRuleResult ruleResult = null;
		Field col = null;
		FieldToNode c2n = null;
		Edge c2n__source__col = null;
		Edge c2n__target__colNode = null;
		Edge rowNode__children__colNode = null;
		Edge col__board__board = null;
		Iterator fujaba__IterBoardToB2f = null;
		BoardToFile b2f = null;
		Board board = null;
		Field row = null;
		Iterator fujaba__IterRowNodeToR2n = null;
		FieldToNode r2n = null;
		File file = null;
		Node rowNode = null;

		// story pattern prepare return value
		try {
			fujaba__Success = false;

			// create object ruleResult
			ruleResult = TGGRuntimeFactory.eINSTANCE.createPerformRuleResult();
			// assign attribute ruleResult
			ruleResult.setSuccess(false);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern find context
		try {
			fujaba__Success = false;

			// check object colNode is really bound
			JavaSDM.ensure(colNode != null);
			// bind object
			rowNode = colNode.getParentNode();

			// check object rowNode is really bound
			JavaSDM.ensure(rowNode != null);

			// check isomorphic binding between objects rowNode and colNode
			JavaSDM.ensure(!rowNode.equals(colNode));

			// bind object
			file = rowNode.getFile();

			// check object file is really bound
			JavaSDM.ensure(file != null);

			// iterate to-many link target from rowNode to r2n
			fujaba__Success = false;

			fujaba__IterRowNodeToR2n = new ArrayList(
					org.moflon.util.eMoflonEMFUtil.getOppositeReference(
							rowNode, FieldToNode.class, "target")).iterator();

			while (fujaba__IterRowNodeToR2n.hasNext()) {
				try {
					r2n = (FieldToNode) fujaba__IterRowNodeToR2n.next();

					// check object r2n is really bound
					JavaSDM.ensure(r2n != null);
					// bind object
					row = r2n.getSource();

					// check object row is really bound
					JavaSDM.ensure(row != null);

					// bind object
					board = row.getBoard();

					// check object board is really bound
					JavaSDM.ensure(board != null);

					// iterate to-many link source from board to b2f
					fujaba__Success = false;

					fujaba__IterBoardToB2f = new ArrayList(
							org.moflon.util.eMoflonEMFUtil
									.getOppositeReference(board,
											BoardToFile.class, "source"))
							.iterator();

					while (fujaba__IterBoardToB2f.hasNext()) {
						try {
							b2f = (BoardToFile) fujaba__IterBoardToB2f.next();

							// check object b2f is really bound
							JavaSDM.ensure(b2f != null);
							// check link target from b2f to file
							JavaSDM.ensure(file.equals(b2f.getTarget()));

							// attribute condition
							JavaSDM.ensure(JavaSDM.stringCompare(
									colNode.getName(), "FIELD") == 0);

							// Create CSP
							CSP csp = CspFactory.eINSTANCE.createCSP();

							// Create literals

							// Create implicit parameters
							Variable<Integer> var_row_y = CspFactory.eINSTANCE
									.createVariable("row.y", true, csp);
							var_row_y.setValue(row.getY());
							Variable<Integer> var_colNode_index = CspFactory.eINSTANCE
									.createVariable("colNode.index", true, csp);
							var_colNode_index.setValue(colNode.getIndex());

							// Create explicit parameters

							// Create unbound variables
							Variable<Integer> var_col_y = CspFactory.eINSTANCE
									.createVariable("col.y", csp);

							// Create constraints
							Eq<Integer> eq = new Eq<Integer>();
							Eq<Integer> eq_0 = new Eq<Integer>();

							csp.getConstraints().add(eq);
							csp.getConstraints().add(eq_0);

							// Solve CSP
							eq.solve(var_row_y, var_col_y);
							eq_0.solve(var_col_y, var_colNode_index);

							fujaba__Success = csp.check();
							if (fujaba__Success) {
								// story pattern perform transformation
								try {
									fujaba__Success = false;

									// check object b2f is really bound
									JavaSDM.ensure(b2f != null);
									// check object board is really bound
									JavaSDM.ensure(board != null);
									// check object colNode is really bound
									JavaSDM.ensure(colNode != null);
									// check object file is really bound
									JavaSDM.ensure(file != null);
									// check object r2n is really bound
									JavaSDM.ensure(r2n != null);
									// check object row is really bound
									JavaSDM.ensure(row != null);
									// check object rowNode is really bound
									JavaSDM.ensure(rowNode != null);
									// check isomorphic binding between objects rowNode and colNode
									JavaSDM.ensure(!rowNode.equals(colNode));

									// check link children from colNode to rowNode
									JavaSDM.ensure(rowNode.equals(colNode
											.getParentNode()));

									// check link fields from row to board
									JavaSDM.ensure(board.equals(row.getBoard()));

									// check link rootNode from file to rowNode
									JavaSDM.ensure(rowNode.equals(file
											.getRootNode()));

									// check link source from b2f to board
									JavaSDM.ensure(board.equals(b2f.getSource()));

									// check link source from r2n to row
									JavaSDM.ensure(row.equals(r2n.getSource()));

									// check link target from b2f to file
									JavaSDM.ensure(file.equals(b2f.getTarget()));

									// check link target from r2n to rowNode
									JavaSDM.ensure(rowNode.equals(r2n
											.getTarget()));

									// attribute condition
									JavaSDM.ensure(JavaSDM.stringCompare(
											colNode.getName(), "FIELD") == 0);

									// create object col
									col = BoardGameFactory.eINSTANCE
											.createField();
									// create object c2n
									c2n = BoardGameToTreeIntegrationFactory.eINSTANCE
											.createFieldToNode();
									// assign attribute col
									col.setY(var_col_y.getValue());
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(c2n, colNode,
													"target");
									// create link
									col.setBoard(board);

									// create link
									c2n.setSource(col);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}

								// story pattern collect translated elements
								try {
									fujaba__Success = false;

									// check object c2n is really bound
									JavaSDM.ensure(c2n != null);
									// check object col is really bound
									JavaSDM.ensure(col != null);
									// check object colNode is really bound
									JavaSDM.ensure(colNode != null);
									// check object ruleResult is really bound
									JavaSDM.ensure(ruleResult != null);
									// assign attribute ruleResult
									ruleResult.setSuccess(true);
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													col, "createdElements");
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													c2n, "createdLinkElements");
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													colNode,
													"translatedElements");
									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}

								// story pattern bookkeeping for edges
								try {
									fujaba__Success = false;

									// check object b2f is really bound
									JavaSDM.ensure(b2f != null);
									// check object board is really bound
									JavaSDM.ensure(board != null);
									// check object c2n is really bound
									JavaSDM.ensure(c2n != null);
									// check object col is really bound
									JavaSDM.ensure(col != null);
									// check object colNode is really bound
									JavaSDM.ensure(colNode != null);
									// check object file is really bound
									JavaSDM.ensure(file != null);
									// check object r2n is really bound
									JavaSDM.ensure(r2n != null);
									// check object row is really bound
									JavaSDM.ensure(row != null);
									// check object rowNode is really bound
									JavaSDM.ensure(rowNode != null);
									// check object ruleResult is really bound
									JavaSDM.ensure(ruleResult != null);
									// check isomorphic binding between objects board and b2f
									JavaSDM.ensure(!board.equals(b2f));

									// check isomorphic binding between objects c2n and b2f
									JavaSDM.ensure(!c2n.equals(b2f));

									// check isomorphic binding between objects col and b2f
									JavaSDM.ensure(!col.equals(b2f));

									// check isomorphic binding between objects colNode and b2f
									JavaSDM.ensure(!colNode.equals(b2f));

									// check isomorphic binding between objects file and b2f
									JavaSDM.ensure(!file.equals(b2f));

									// check isomorphic binding between objects r2n and b2f
									JavaSDM.ensure(!r2n.equals(b2f));

									// check isomorphic binding between objects row and b2f
									JavaSDM.ensure(!row.equals(b2f));

									// check isomorphic binding between objects rowNode and b2f
									JavaSDM.ensure(!rowNode.equals(b2f));

									// check isomorphic binding between objects c2n and board
									JavaSDM.ensure(!c2n.equals(board));

									// check isomorphic binding between objects col and board
									JavaSDM.ensure(!col.equals(board));

									// check isomorphic binding between objects colNode and board
									JavaSDM.ensure(!colNode.equals(board));

									// check isomorphic binding between objects file and board
									JavaSDM.ensure(!file.equals(board));

									// check isomorphic binding between objects r2n and board
									JavaSDM.ensure(!r2n.equals(board));

									// check isomorphic binding between objects row and board
									JavaSDM.ensure(!row.equals(board));

									// check isomorphic binding between objects rowNode and board
									JavaSDM.ensure(!rowNode.equals(board));

									// check isomorphic binding between objects col and c2n
									JavaSDM.ensure(!col.equals(c2n));

									// check isomorphic binding between objects colNode and c2n
									JavaSDM.ensure(!colNode.equals(c2n));

									// check isomorphic binding between objects file and c2n
									JavaSDM.ensure(!file.equals(c2n));

									// check isomorphic binding between objects r2n and c2n
									JavaSDM.ensure(!r2n.equals(c2n));

									// check isomorphic binding between objects row and c2n
									JavaSDM.ensure(!row.equals(c2n));

									// check isomorphic binding between objects rowNode and c2n
									JavaSDM.ensure(!rowNode.equals(c2n));

									// check isomorphic binding between objects colNode and col
									JavaSDM.ensure(!colNode.equals(col));

									// check isomorphic binding between objects file and col
									JavaSDM.ensure(!file.equals(col));

									// check isomorphic binding between objects r2n and col
									JavaSDM.ensure(!r2n.equals(col));

									// check isomorphic binding between objects row and col
									JavaSDM.ensure(!row.equals(col));

									// check isomorphic binding between objects rowNode and col
									JavaSDM.ensure(!rowNode.equals(col));

									// check isomorphic binding between objects file and colNode
									JavaSDM.ensure(!file.equals(colNode));

									// check isomorphic binding between objects r2n and colNode
									JavaSDM.ensure(!r2n.equals(colNode));

									// check isomorphic binding between objects row and colNode
									JavaSDM.ensure(!row.equals(colNode));

									// check isomorphic binding between objects rowNode and colNode
									JavaSDM.ensure(!rowNode.equals(colNode));

									// check isomorphic binding between objects r2n and file
									JavaSDM.ensure(!r2n.equals(file));

									// check isomorphic binding between objects row and file
									JavaSDM.ensure(!row.equals(file));

									// check isomorphic binding between objects rowNode and file
									JavaSDM.ensure(!rowNode.equals(file));

									// check isomorphic binding between objects row and r2n
									JavaSDM.ensure(!row.equals(r2n));

									// check isomorphic binding between objects rowNode and r2n
									JavaSDM.ensure(!rowNode.equals(r2n));

									// check isomorphic binding between objects rowNode and row
									JavaSDM.ensure(!rowNode.equals(row));

									// create object c2n__source__col
									c2n__source__col = TGGRuntimeFactory.eINSTANCE
											.createEdge();
									// create object c2n__target__colNode
									c2n__target__colNode = TGGRuntimeFactory.eINSTANCE
											.createEdge();
									// create object rowNode__children__colNode
									rowNode__children__colNode = TGGRuntimeFactory.eINSTANCE
											.createEdge();
									// create object col__board__board
									col__board__board = TGGRuntimeFactory.eINSTANCE
											.createEdge();
									// assign attribute rowNode__children__colNode
									rowNode__children__colNode
											.setName("children");
									// assign attribute col__board__board
									col__board__board
											.setName("col__board__board");
									// assign attribute c2n__source__col
									c2n__source__col
											.setName("c2n__source__col");
									// assign attribute c2n__target__colNode
									c2n__target__colNode
											.setName("c2n__target__colNode");
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													c2n__source__col,
													"createdEdges");
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													c2n__target__colNode,
													"createdEdges");
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													rowNode__children__colNode,
													"translatedEdges");
									// create link
									org.moflon.util.eMoflonEMFUtil
											.addOppositeReference(ruleResult,
													col__board__board,
													"createdEdges");
									// create link
									rowNode__children__colNode.setSrc(rowNode);

									// create link
									col__board__board.setTrg(board);

									// create link
									col__board__board.setSrc(col);

									// create link
									c2n__source__col.setTrg(col);

									// create link
									c2n__target__colNode.setSrc(c2n);

									// create link
									c2n__source__col.setSrc(c2n);

									// create link
									c2n__target__colNode.setTrg(colNode);

									// create link
									rowNode__children__colNode.setTrg(colNode);

									fujaba__Success = true;
								} catch (JavaSDMException fujaba__InternalException) {
									fujaba__Success = false;
								}

								return ruleResult;

							}

							fujaba__Success = true;
						} catch (JavaSDMException fujaba__InternalException) {
							fujaba__Success = false;
						}
					}
					JavaSDM.ensure(fujaba__Success);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return ruleResult;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public IsAppropriateRuleResult isAppropriate_BWD_Node_1(Node colNode) {
		boolean fujaba__Success = false;
		Object _TmpObject = null;
		EClass eClass = null;
		Iterator fujaba__IterEClassToPerformOperation = null;
		EOperation performOperation = null;
		IsAppropriateRuleResult ruleResult = null;
		Match match = null;
		Edge null__children__colNode = null;
		File file = null;
		Node rowNode = null;
		Iterator fujaba__IterRuleResultToMatch = null;

		// story pattern prepare return value
		try {
			fujaba__Success = false;

			_TmpObject = (this.eClass());

			// ensure correct type and really bound of object eClass
			JavaSDM.ensure(_TmpObject instanceof EClass);
			eClass = (EClass) _TmpObject;

			// iterate to-many link eOperations from eClass to performOperation
			fujaba__Success = false;

			fujaba__IterEClassToPerformOperation = eClass.getEOperations()
					.iterator();

			while (!(fujaba__Success)
					&& fujaba__IterEClassToPerformOperation.hasNext()) {
				try {
					performOperation = (EOperation) fujaba__IterEClassToPerformOperation
							.next();

					// check object performOperation is really bound
					JavaSDM.ensure(performOperation != null);
					// attribute condition
					JavaSDM.ensure(JavaSDM.stringCompare(
							performOperation.getName(), "perform_BWD_Node_1") == 0);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// create object ruleResult
			ruleResult = TGGRuntimeFactory.eINSTANCE
					.createIsAppropriateRuleResult();
			// assign attribute ruleResult
			ruleResult.setSuccess(false);
			// create link
			ruleResult.setPerformOperation(performOperation);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern test core match
		try {
			fujaba__Success = false;

			// check object colNode is really bound
			JavaSDM.ensure(colNode != null);
			// check object ruleResult is really bound
			JavaSDM.ensure(ruleResult != null);
			// bind object
			rowNode = colNode.getParentNode();

			// check object rowNode is really bound
			JavaSDM.ensure(rowNode != null);

			// check isomorphic binding between objects rowNode and colNode
			JavaSDM.ensure(!rowNode.equals(colNode));

			// bind object
			file = rowNode.getFile();

			// check object file is really bound
			JavaSDM.ensure(file != null);

			// attribute condition
			JavaSDM.ensure(JavaSDM.stringCompare(colNode.getName(), "FIELD") == 0);

			// create object match
			match = TGGRuntimeFactory.eINSTANCE.createMatch();
			// create link
			ruleResult.getMatches().add(match);

			// story pattern collect elements to be translated
			try {
				fujaba__Success = false;

				// check object colNode is really bound
				JavaSDM.ensure(colNode != null);
				// check object match is really bound
				JavaSDM.ensure(match != null);
				// check object rowNode is really bound
				JavaSDM.ensure(rowNode != null);
				// check isomorphic binding between objects rowNode and colNode
				JavaSDM.ensure(!rowNode.equals(colNode));

				// create object null__children__colNode
				null__children__colNode = TGGRuntimeFactory.eINSTANCE
						.createEdge();
				// assign attribute null__children__colNode
				null__children__colNode.setName("children");
				// create link
				org.moflon.util.eMoflonEMFUtil.addOppositeReference(match,
						colNode, "toBeTranslatedElements");
				// create link
				org.moflon.util.eMoflonEMFUtil.addOppositeReference(match,
						null__children__colNode, "toBeTranslatedEdges");
				// create link
				null__children__colNode.setTrg(colNode);

				// create link
				null__children__colNode.setSrc(rowNode);

				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			// story pattern collect context nodes
			try {
				fujaba__Success = false;

				// check object file is really bound
				JavaSDM.ensure(file != null);
				// check object match is really bound
				JavaSDM.ensure(match != null);
				// check object rowNode is really bound
				JavaSDM.ensure(rowNode != null);
				// create link
				org.moflon.util.eMoflonEMFUtil.addOppositeReference(match,
						rowNode, "contextElements");
				// create link
				org.moflon.util.eMoflonEMFUtil.addOppositeReference(match,
						file, "contextElements");
				fujaba__Success = true;
			} catch (JavaSDMException fujaba__InternalException) {
				fujaba__Success = false;
			}

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern set success
		try {
			fujaba__Success = false;

			// check object ruleResult is really bound
			JavaSDM.ensure(ruleResult != null);
			// iterate to-many link matches from ruleResult to match
			fujaba__Success = false;

			fujaba__IterRuleResultToMatch = ruleResult.getMatches().iterator();

			while (!(fujaba__Success)
					&& fujaba__IterRuleResultToMatch.hasNext()) {
				try {
					match = (Match) fujaba__IterRuleResultToMatch.next();

					// check object match is really bound
					JavaSDM.ensure(match != null);

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			// assign attribute ruleResult
			ruleResult.setSuccess(true);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return ruleResult;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments)
			throws InvocationTargetException {
		switch (operationID) {
		case RulesPackage.ROW_FIELD_RULE___PERFORM_FWD_FIELD_1__FIELD:
			return perform_FWD_Field_1((Field) arguments.get(0));
		case RulesPackage.ROW_FIELD_RULE___IS_APPROPRIATE_FWD_FIELD_1__FIELD:
			return isAppropriate_FWD_Field_1((Field) arguments.get(0));
		case RulesPackage.ROW_FIELD_RULE___PERFORM_BWD_NODE_1__NODE:
			return perform_BWD_Node_1((Node) arguments.get(0));
		case RulesPackage.ROW_FIELD_RULE___IS_APPROPRIATE_BWD_NODE_1__NODE:
			return isAppropriate_BWD_Node_1((Node) arguments.get(0));
		}
		return super.eInvoke(operationID, arguments);
	}

} //RowFieldRuleImpl
